package httputils

import (
	"fmt"
	"io"
	"net/http"
	"regexp"
)

// fetchData 获取数据并处理JSONP响应
func GetByUrl(url string) ([]byte, []byte, error) {
	referer := "https://data.eastmoney.com/bkzj/gn.html"

	client := &http.Client{}
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		fmt.Println("创建请求失败:", url, nil)
		return nil, nil, err
	}
	req.Header.Add("Referer", referer)
	req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")
	resp, err := client.Do(req)
	fmt.Println(url)
	if err != nil {
		return nil, nil, err
	}
	defer resp.Body.Close()

	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return nil, nil, err
	}

	// 处理JSONP响应：提取JSON部分
	re := regexp.MustCompile(`\w+\(([\s\S]*)\);?$`)
	matches := re.FindStringSubmatch(string(body))

	var jsonData string
	if len(matches) > 1 {
		jsonData = matches[1]
	} else {
		jsonData = string(body)
	}
	// fmt.Println(string(jsonData))

	return []byte(jsonData), body, nil
}
